<extend name='Public/index'/>
<block name='content'>
<table id='Tbl'>
<thead>
	<tr>
		<th><input type="checkbox" onclick="selectAll(this)" value='全选'></th>
		<th>编号</th>
		<th>规则名称</th>
		<th>分值</th>
		<th>描述</th>
		<th>锁定</th>
		<th>操作</th>
	</tr>
	</thead>
	<tbody>
	<volist name='points_rule' id='vo'>
		<tr id="row_<{$vo.point_rule_id}>">
			<td><input type="checkbox" class='select' value='<{$vo.point_rule_id}>' ></td>
			<td><{$vo.point_rule_id}></td>
			<td><input name="rule_name" class="clss" disabled type="text" value="<{$vo.rule_name}>"></td>
			<td><input name="val" class="clss" disabled type="text" value="<{$vo.val}>"></td>
			<td><input name="desc" class="clss" disabled  type="text" value="<{$vo.desc}>"></td>
                
			<td>
			
             <div class="switch tiny">
              <input onclick="lock(this)" value="<{$vo.point_rule_id}>" id="lock_<{$vo.point_rule_id}>" type="checkbox"  <{$vo['lock']?'checked':''}>>
                <label for="lock_<{$vo.point_rule_id}>"></label>
                </div>
               
			</td>
            <td>
			<button type="button" onclick='edit("<{$vo.point_rule_id}>")' class='edit'>编辑</button>
			<button style='display:none' onclick='save("<{$vo.point_rule_id}>")' type="button" class="save">保存</button>
			</td>
		</tr>
	</volist>
	</tbody>
</table>
<input type="button" class="button tiny" onclick="moreDel()" value='删除(批量)'>
<input type="button" class="button tiny" onclick="unSelect()" value='反选'>
<input type="button" class='button tiny' onclick="addMenu()" value='添加'>
<script>
        function selectAll(obj) {
            $('#Tbl tbody .select').prop('checked',$(obj).prop('checked'));
        }
        function unSelect() {
            $('#Tbl tbody .select').map(function(){
                $(this).prop('checked',!$(this).prop('checked'));
            });
        }
        function moreDel() {
            if(!confirm('确定删除吗?')){
                return;
            }
            var ids = $('#Tbl tbody .select:checked').map(function () {
                $(this).parent().parent().remove();
                return $(this).val();
            });
            ids  = ids.get().join();
            $.post('<{:U("doDelPointsRule")}>',{ids:ids},function (data) {
                if(data.error ==0)alert(data.msg);
            });
        }
        function lock(obj) {
            var id = $(obj).val();
            var lockval = $(obj).prop('checked')?1:0;
            $.post('<{:U("doEditPointsRule")}>',{point_rule_id:id,lock:lockval});
        }

        
        
        function edit(id) {
            $('#row_'+id+' .clss').prop('disabled',false);
            $('#row_'+id+' .edit').hide();
            $('#row_'+id+' .save').show();
        }

        function save(id) {
            $('#row_'+id+' .clss').prop('disabled',true);
            $('#row_'+id+' .edit').show();
            $('#row_'+id+' .save').hide();

            var data = {
                point_rule_id:id,
                rule_name:$('#row_'+id+' [name=rule_name]').val(),
                val:  $('#row_'+id+' [name=val]').val(),
                desc :  $('#row_'+id+' [name=desc]').val(),
            }

            $.post('<{:U("doEditPointsRule")}>',data,function (data) {
                if(data.error==0){
                    alert(data.msg);
                }
            });

        }
         function addMenu(){
            $.get("<{:U('lastId')}>", '',
            function(id){
               var id= parseInt(id)+1;

                var html =
            '<tr id="row_'+id+'">'+
'            <td><input type="checkbox" class="select" value="" ></td>'+
'            <td>'+id+'</td>'+
'            <td><input name="rule_name" class="clss" disabled type="text" value=""></td>'+
'            <td><input name="val" class="clss" disabled type="text" value=""></td>'+
'            <td><input name="desc" class="clss" disabled type="text" value=""></td>'+
'            <td>'+
'             <div class="switch tiny">'+
'              <input onclick="lock(this)" value="" id="lock_'+id+'" type="checkbox"  <{$vo["lock"]?"checked":""}>>'+
'                <label for="lock_'+id+'"></label>'+
'                </div>'+
'               '+
'            </td> '+
'            <td>'+
'            <button type="button" onclick="edit(\''+id+'\')" class="edit tiny">编辑</button>'+
'            <button style="display:none" onclick="save(\''+id+'\')" type="button" class="save tiny">保存</button>'+
'            </td>'+
'        </tr>';
 $('tbody').append(html);
            
            });

        }
</script>
</div>
</block>